﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;

namespace TibcoMessageAdmin
{
    public class Util
    {

        public static List<Queue> GetQueueList()
        {
            List<Queue> queueList = new List<Queue>(); 
            string file = Application.StartupPath +"\\"  + "config.xml"  ;

            try
            {

                XDocument loaded = XDocument.Load(file);
                var q = from c in loaded.Descendants("QueueDetail")
                        select c;


                foreach (var x in q)
                {
                    try
                    {
                        Queue qu = new Queue();
                        qu.ID = x.FirstAttribute.Value; 
                        qu.Server = x.Element("Server").Value;
                        qu.QueueName = x.Element("Queue").Value;
                        qu.User = x.Element("User").Value;
                        qu.Password = x.Element("Password").Value;
                        queueList.Add(qu);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                loaded = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            
            return queueList;
        }


        public static void LoadQueues(ComboBox cboConfig, ref List<Queue> queueList)
        {
            cboConfig.Items.Clear();
            queueList = Util.GetQueueList();
            foreach (Queue q in queueList)
            {
                QueueDetail qd = new QueueDetail();
                qd.Display = "[Server]=" + q.Server + "/[Queue]=" + q.QueueName;
                qd.ID = q.ID;
                cboConfig.Items.Add(qd);
            }
            if (cboConfig.Items.Count > 0)
            {
                cboConfig.SelectedIndex = 0;
            }
        }
    }
}
